/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.sql.SQLException;

import blueprint4j.utils.Log;


/**
 * This class represents an action, actions are added to data groups, the entity that
 * constructs this field must implements actionable
 * When the action (button) is pressed the actionable entity will be called
 */
public class FieldAction extends FieldBoolean {
	private static final long serialVersionUID = 1L;

	private String image= null;
    private Actionable actionable = null;
//    private Binder binder = null;
//    private EntityPanel entity_panel = null;
    
    public FieldAction(String name,String description,String image,Actionable actionable) {
        super(name,Field.FIELD_NOT_REAL,description,actionable.getEntity());
        actionable.getEntity().addFieldAction(this);
        this.actionable = actionable;
        this.image = image;
    }
    
//    public void setBinder(Binder binder) {
//        this.binder = binder;
//    }
//    
//    public Binder getBinder() {
//        return binder;
//    }
//    
//    public void setEntityPanel(EntityPanel entity_panel) {
//        this.entity_panel = entity_panel;
//    }
//    
//    /**
//     *  This Will Refresh the Entity Panel
//     */
//    public void refresh() throws BindException {
//        getBinder().reload();
//        if (entity_panel != null) {
//            entity_panel.actionRefresh();
//        }
//    }
    
    public String getImagePath(){
    	return image;
    }
    
//    public JButton createButton() {
//        JButton btn = null;
//        if (image != null) {
//            try {
//                btn = new JButton(Constants.loadIconsFromResource(new String[]{image})[0]);
//            } catch (Exception ioe){
//                Log.critical.out(ioe);
//            }
//        } else {
//            btn = new JButton(getDescription());
//        }
//        return btn;
//    }
    
    public FieldAction(String name,String description,Actionable actionable) {
        this(name,description,null,actionable);
    }
    
    public Actionable getActionable() {
        return actionable;
    }
    
    public void action() {
        try {
            actionable.action(this);
        } catch (Throwable th) {
            Log.debug.out(th);
            javax.swing.JOptionPane.showMessageDialog(null,th.getMessage());
        }
    }
    
    public boolean isActionAble()
    throws SQLException {
        return actionable.isActionable(this);
    }
    
    public static interface Actionable {
        public void action(FieldAction event) throws Throwable;
        
        public Entity getEntity();
                /*
                 * The action might not be valid so it should not be editable
                 */
        public boolean isActionable(FieldAction action);
    }
}
